package org.oos.atom.entries.impl;

import static org.oos.atom.elements.Category.Type.LISTS;
import static org.oos.atom.elements.Category.Type.PRIVACY;
import static org.oos.atom.elements.Category.Type.TAGS;

import java.util.Date;
import java.util.List;

import org.oos.atom.elements.Category;
import org.oos.atom.elements.Rating;
import org.oos.atom.entries.ReviewEntry;

public class ReviewEntryImpl extends SiteEntryImpl implements ReviewEntry,
		Cloneable {

	private static final long serialVersionUID = -7123758044485885032L;

	private Category privacy;
	private List<Category> tags;
	private List<Category> lists;

	@Override
	public void setRating(Rating rating) {
		this.rating = rating;
	}

	@Override
	public Date getDate() {
		return super.getUpdated();
	}

	@Override
	public Category getPrivacy() {
		if (privacy == null) {
			List<Category> cats = Category.find(this.categories, PRIVACY);
			if (cats.size() > 0) {
				privacy = cats.get(0);
			}
		}
		return privacy;
	}

	@Override
	public void setPrivacy(Category privacy) {
		this.categories = Category.delete(this.categories, PRIVACY);
		this.categories.add(privacy);
		this.privacy = privacy;
	}

	@Override
	public List<Category> getTags() {
		if (tags == null) {
			tags = Category.find(this.categories, TAGS);
		}
		return tags;
	}

	@Override
	public void setTags(List<Category> tags) {
		this.categories = Category.delete(this.categories, TAGS);
		this.categories.addAll(tags);
		this.tags = tags;
	}

	@Override
	public void addTag(Category tag) {
		this.categories.remove(tag);
		this.categories.add(tag);

		getTags().remove(tag);
		getTags().add(tag);
	}

	@Override
	public List<Category> getLists() {
		if (lists == null) {
			lists = Category.find(this.categories, LISTS);
		}
		return lists;
	}

	@Override
	public void setLists(List<Category> lists) {
		this.categories = Category.delete(this.categories, LISTS);
		this.categories.addAll(lists);
		this.lists = lists;
	}

	@Override
	public void addList(Category list) {
		this.categories.remove(list);
		this.categories.add(list);

		getLists().remove(list);
		getLists().add(list);
	}

	@Override
	public ReviewEntryImpl clone() throws CloneNotSupportedException {
		return (ReviewEntryImpl) super.clone();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj instanceof ReviewEntry) {
			ReviewEntry other = (ReviewEntry) obj;
			if (other.getSlug() == null || getSlug() == null
					|| getAuthor().getSlug() == null
					|| other.getAuthor().getSlug() == null) {
				return false;
			}
			return other.getSlug().equals(getSlug())
					&& other.getAuthor().getSlug()
							.equals(getAuthor().getSlug());
		}
		return false;
	}
}
